package abq2.jic.jic.plugin.operate.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class MyCourseInfo implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String, String> result = new HashMap<>();
        if ("GET_Course_Info".equalsIgnoreCase(action)) {
            //获取DynamicObject列表，即获取学生的单据头的信息
            DynamicObject[] dys = BusinessDataServiceHelper.load("abq2_my_course",
                            "number," +
                            "name," +          //名称
                            "abq2_textfield," +            //教师
                            "abq2_textfield1," +     //教室
                            "abq2_textfield2," +      //课节
                            "abq2_textfield3,"  + //星期
                            "creator",           //学生
                    (new QFilter("creator", QCP.equals, RequestContext.get().getCurrUserId())).toArray());

            //创建一个JsonArray
            JSONArray jsonArray = new JSONArray();
            for (DynamicObject dynamicObject : dys) {
                //将学生的考试基本的信息加入JSONArray
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("courseName", dynamicObject.getString("name"));
                jsonObject.put("teacher", dynamicObject.getString("abq2_textfield"));
                jsonObject.put("classroom", dynamicObject.getString("abq2_textfield1"));
                String[] classTimes = dynamicObject.getString("abq2_textfield2").split(",");
                String stuClassTimes = "";
                for (String classTime : classTimes) {
                    stuClassTimes += "第" + classTime + "节课，";
                }
                jsonObject.put("classTime", stuClassTimes);
                jsonObject.put("week", dynamicObject.getString("abq2_textfield3"));
                jsonArray.add(jsonObject);
            }


            System.out.println(jsonArray.toJSONString());
            //加入resultDynamicObject参数，将JsonArray加入到这个参数当中，然后返回
            result.put("resultDynamicObject", jsonArray.toJSONString());
        }
            return result;
        }
}
